package com.ruoyi.project.system.categoryuser.controller;

import java.util.List;

import com.ruoyi.project.system.appsys.domain.AppSys;
import com.ruoyi.project.system.appsys.service.IAppSysService;
import com.ruoyi.project.system.categoryuser.dto.CategoryUserDTO;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.categoryuser.domain.CategoryUser;
import com.ruoyi.project.system.categoryuser.service.ICategoryUserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 用户自定义的分类Controller
 *
 * @author ruoyi
 * @date 2021-05-24
 */
@Controller
@RequestMapping("/system/categoryuser")
public class CategoryUserController extends BaseController {
    private String prefix = "system/categoryuser";

    @Autowired
    private ICategoryUserService categoryUserService;

    @Autowired
    private IAppSysService appSysService;

    @RequiresPermissions("system:categoryuser:view")
    @GetMapping()
    public String categoryuser() {
        return prefix + "/categoryuser";
    }

    /**
     * 查询用户自定义的分类列表
     */
    @RequiresPermissions("system:categoryuser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CategoryUser categoryUser) {
        startPage();
        List<CategoryUser> list = categoryUserService.selectCategoryUserList(categoryUser);
        return getDataTable(list);
    }

    /**
     * 导出用户自定义的分类列表
     */
    @RequiresPermissions("system:categoryuser:export")
    @Log(title = "用户自定义的分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CategoryUser categoryUser) {
        List<CategoryUser> list = categoryUserService.selectCategoryUserList(categoryUser);
        ExcelUtil<CategoryUser> util = new ExcelUtil<CategoryUser>(CategoryUser.class);
        return util.exportExcel(list, "用户自定义的分类数据");
    }

    /**
     * 新增用户自定义的分类
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存用户自定义的分类
     */
    @RequiresPermissions("system:categoryuser:add")
    @Log(title = "用户自定义的分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CategoryUserDTO categoryUserDTO) {
        String appids = categoryUserDTO.getAppids();
        if (appids != null && !"".equals(appids)) {
            String[] split = appids.split(",");
            for (String s : split) {
                CategoryUser categoryUser = new CategoryUser();
                AppSys appSys = appSysService.selectAppSysById(Long.parseLong(s));
                if (appSys != null) {
                    categoryUser.setUserId(appSys.getId());
                    categoryUser.setName(appSys.getName());
                    categoryUserService.insertCategoryUser(categoryUser);
                }
            }
        }

        return toAjax(1);
    }

    /**
     * 修改用户自定义的分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CategoryUser categoryUser = categoryUserService.selectCategoryUserById(id);
        mmap.put("categoryUser", categoryUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户自定义的分类
     */
    @RequiresPermissions("system:categoryuser:edit")
    @Log(title = "用户自定义的分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CategoryUser categoryUser) {
        return toAjax(categoryUserService.updateCategoryUser(categoryUser));
    }

    /**
     * 删除用户自定义的分类
     */
    @RequiresPermissions("system:categoryuser:remove")
    @Log(title = "用户自定义的分类", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(categoryUserService.deleteCategoryUserByIds(ids));
    }

    /**
     * 状态修改
     */
    @Log(title = "用户自定义的分类", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:categoryuser:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(CategoryUser categoryUser)
    {
        return toAjax(categoryUserService.changeStatus(categoryUser));
    }
}
